﻿using System.Windows;
using System.Windows.Data;
using System.Windows.Controls;
using System.ComponentModel;
using System.IO;
using System.Windows.Resources;
using System;
using System.Net;

namespace DemoDataGridToolBar.Views
{
	public partial class LocalData : Page
	{
		public LocalData()
		{
			InitializeComponent();
			Loaded += new RoutedEventHandler(ProductsUC_Loaded1);
		}

		public ProductsLocal li_products;

		void ProductsUC_Loaded1(object sender, RoutedEventArgs e)
		{
			li_products = new ProductsLocal();
			// Get data from Web
			WebClient webClient = new WebClient();
			webClient.DownloadStringCompleted += (o, a) => {
				if(a.Error == null) {
					using(StringReader sr = new StringReader(a.Result)) {
						string row = sr.ReadLine(); // no first row
						while((row = sr.ReadLine()) != null) {
							string[] ass = row.Split(';');

							decimal standardCost, listPrice, weight;
							int size;
							decimal.TryParse(ass[4], System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture, out standardCost);
							decimal.TryParse(ass[5], System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture, out listPrice);
							decimal.TryParse(ass[7], System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture, out weight);
							int.TryParse(ass[6], System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture, out size);
							ProductLocal product = new ProductLocal() {
								ProductID = Guid.NewGuid(),
								Name = ass[1],
								ProductNumber = ass[2],
								Color = ass[3],
								StandardCost = standardCost, //ass[4],
								ListPrice = listPrice, //ass[5],
								Size = size, // ass[6],
								Weight = weight, //ass[7],
								SellStartDate = DateTime.Parse(ass[8]),
							};
							li_products.Add(product);
						}
					}
					//-- 
					ICollectionView collectionView = collectionView = new PagedCollectionView(li_products);
					dgProducts.ItemsSource = collectionView;
				}
			};
			webClient.DownloadStringAsync(new Uri("Product.csv", UriKind.Relative));
		}


		void ProductsUC_Loaded2(object sender, RoutedEventArgs e)
		{
			//-- get list Products from Resource "content"
			li_products = new ProductsLocal();
			StreamResourceInfo imageResourceInfo = Application.GetResourceStream(new Uri("Product.csv", UriKind.Relative));


			using(var sr = new StreamReader(imageResourceInfo.Stream)) {
				sr.ReadLine(); // no first row
				while(!sr.EndOfStream) {
					string[] ass = sr.ReadLine().Split(';');

					decimal standardCost, listPrice, weight;
					int size;
					decimal.TryParse(ass[4], System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture, out standardCost);
					decimal.TryParse(ass[5], System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture, out listPrice);
					decimal.TryParse(ass[7], System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture, out weight);
					int.TryParse(ass[6], System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture, out size);
					ProductLocal product = new ProductLocal() {
						ProductID = Guid.NewGuid(),
						Name = ass[1],
						ProductNumber = ass[2],
						Color = ass[3],
						StandardCost = standardCost, //ass[4],
						ListPrice = listPrice, //ass[5],
						Size = size, // ass[6],
						Weight = weight, //ass[7],
						SellStartDate = DateTime.Parse(ass[8]),
					};
					li_products.Add(product);
				}
			}
			//-- 
			ICollectionView collectionView = collectionView = new PagedCollectionView(li_products);
			dgProducts.ItemsSource = collectionView;
			//dgProducts.ItemsSource = li_products;
		}

		private void DataGridToolBar_CreateDataForm(object sender, DataFormEventArgs e)
		{
			e.DataForm = new ProductForm_local();
			//e.DataForm = new ProductForm_localDF();
		}

	}
}
